/*
 * @Author: lineCode 1145938682@qq.com
 * @Date: 2024-12-27 13:38:19
 * @LastEditors: lineCode
 * @LastEditTime: 2025-06-12 12:47:10
 * @FilePath: /backend/shorthand-modules/shorthand-modules-content/src/main/java/com/angus/modules/content/controller/work/SysOrderController.java
 */
package com.angus.modules.content.controller.work;


import com.angus.common.core.web.controller.BaseController;
import com.angus.common.core.web.domain.AjaxResult;
import com.angus.common.security.utils.SecurityUtils;
import com.angus.modules.content.domain.SysOrder;
import com.angus.modules.content.dto.CreateOrderDto;
import com.angus.modules.content.dto.QueryOrderDto;
import com.angus.modules.content.service.SysOrderService;
import com.angus.modules.content.repository.SysOrderRepository;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lineCode
 */
@RestController
@RequestMapping("/order")
public class SysOrderController extends BaseController {

    @Resource
    private SysOrderService sysOrderService;

    @Resource
    private SysOrderRepository sysOrderRepository;

    @GetMapping("/getOrderList")
    public AjaxResult getOrderList(QueryOrderDto dto) {
        Long userId = SecurityUtils.getUserId();
        return AjaxResult.success(sysOrderService.getOrderList(dto,userId));
    }

    @GetMapping("/getPendingOrderList")
    public AjaxResult getPendingOrderList(QueryOrderDto dto) {
        Long userId = SecurityUtils.getUserId();
        return AjaxResult.success(sysOrderService.getPendingOrderList(dto.getCourseCode(),userId));
    }

    @PostMapping("/createOrder")
    public AjaxResult createOrder(@RequestBody CreateOrderDto dto){
        Long userId = SecurityUtils.getUserId();
        return AjaxResult.success(sysOrderService.createOrder(dto, userId));
    }

    @GetMapping("/getOrderDetail")
    public AjaxResult getOrderDetail(@RequestParam("orderNo") String orderNo){
        SysOrder order = sysOrderRepository.getOrder(orderNo);
        return AjaxResult.success(order);
    }

    @DeleteMapping("/delete")
    public AjaxResult deleteOrder(@RequestBody List<Long> ids){
        if (ids == null || ids.isEmpty()) {
            return AjaxResult.warn("ID列表不能为空");
        }
        if (sysOrderService.deleteByIds(ids)){
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }

    /**
     * 取消订单
     * @param orderNo 订单号
     * @return
     */
    @GetMapping("/cancelOrder")
    public AjaxResult cancelOrder(@RequestParam("orderNo") String orderNo){
        sysOrderRepository.updateOrderCancelStatus(orderNo);
        return AjaxResult.success("ok");
    }
}
